package cn.fl.capital.controller;

import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.capital.condition.CapPayListRepQueryCondition;
import cn.fl.capital.service.ICapPayListRepService;
import cn.fl.capital.vo.CapPayListRepVO;
import cn.fl.customer.service.ICstBscInfoService;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.project.service.IPrjBscInfoService;
import cn.fl.system.constant.SysLogConstant;
import cn.fl.system.service.ISysOrgService;
import cn.fl.system.vo.SysOrgVO;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import net.sf.json.JSONArray;
import org.apache.poi.hssf.usermodel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;

/**
 * @Description: 资金放款报表 控制层
 */
@Controller
@RequestMapping("/capital/capPayListRep")
public class CapPayListRepController extends BaseController {
	private static final Logger logger = LoggerFactory.getLogger(CapPayListRepController.class);

    /**
     * CapPayListRep 服务层BEAN
     */
    @Autowired
    private ICapPayListRepService capPayListRepService;

    @Autowired
    private ISysOrgService sysOrgService;

    @Autowired
    private IPrjBscInfoService prjBscInfoService;
    /**
     * 客户 服务层BEAN
     */
    @Autowired
    private ICstBscInfoService cstBscInfoService;


    /**
     * 到列表页面
     */
    @RequestMapping(value = "/toListPage.do", method = {RequestMethod.GET})
    public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
        BaseLogUtils.info(logger, "toListPage", "加载CapPayListRep列表页面.", CurrentThreadContext.getCurrentUserName());
        ModelAndView mav = new ModelAndView("capital/capPayListRep/capPayListRepList");
        //项目类型
        List<KeyValueVO> projectTypeList = this.prjBscInfoService.getPrjTypCdALLKV();
        //业务类型
        //List<KeyValueVO> bizTypeList = this.prjBscInfoService.getBizTypeList();
        // 事业部
        List<KeyValueVO> orgList = this.cstBscInfoService.searchBusinessOrgList();

        //省公司
        List<SysOrgVO> sysOrgVOList = this.sysOrgService.searchListByOrgTypCd("3", "11000");
        List<KeyValueVO> sysOrgListResult = new LinkedList<KeyValueVO>();
        for (SysOrgVO vo : sysOrgVOList) {
            KeyValueVO keyValueVO = new KeyValueVO();
            keyValueVO.setKey(vo.getOrgCd());
            keyValueVO.setValue(vo.getOrgDispNm());
            sysOrgListResult.add(keyValueVO);
        }

        mav.addObject("prjTypCdList", JSON.toJSONString(projectTypeList));
        mav.addObject("buOrgCdList", JSON.toJSONString(orgList));
        mav.addObject("sysOrgVOList", JSON.toJSONString(sysOrgListResult));
        return mav;
    }

    /**
     * 到新增页面
     */
    @RequestMapping(value = "/toAddPage.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView toAddPage(HttpServletRequest request, HttpServletResponse response) {
        BaseLogUtils.info(logger, "toAddPage", "加载CapPayListRep Add页面.", CurrentThreadContext.getCurrentUserName());
        ModelAndView mav = new ModelAndView("capital/capPayListRep/capPayListRepModify");
        //this.prepare(request,mav);
        return mav;
    }

    /**
     * 到修改页面
     */
    @RequestMapping(value = "/toModifyPage.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView toModifyPage(HttpServletRequest request, HttpServletResponse response) {
        BaseLogUtils.info(logger, "toModifyPage", "加载CapPayListRep Modify页面.", CurrentThreadContext.getCurrentUserName());
        ModelAndView mav = new ModelAndView("capital/capPayListRep/capPayListRepModify");
        //this.prepare(request,mav);

        String id = request.getParameter("id");
        CapPayListRepVO capPayListRepVO = this.capPayListRepService.selectById(Long.valueOf(id));
        mav.addObject("capPayListRepVO", capPayListRepVO);
        return mav;
    }

    //加载数据字典数据、传值等 ？
    public void prepare(HttpServletRequest request, ModelAndView mav) {

    }

    /**
     * 初始化列表
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/initList.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object initCapPayListRepList(HttpServletRequest request, HttpServletResponse response) {
        String currentPage = request.getParameter("page");
        String pageSize = request.getParameter("pagesize");
        String cstNm = request.getParameter("cstNm");
        String cstMgrOrgCdNm = request.getParameter("cstMgrOrgCdNm");
        String buOrgCd = request.getParameter("buOrgCd");
        String prjTypCd = request.getParameter("prjTypCd");
        String paySchNo = request.getParameter("paySchNo");
        String prInsNo = request.getParameter("prInsNo");
        String crtTmMin = request.getParameter("crtTmMin");
        String crtTmMax = request.getParameter("crtTmMax");
        Pager page = new Pager();
        if (StringUtils.isNotEmpty(currentPage)) {
            page.setCurrentPage(Integer.parseInt(currentPage));
        }

        if (StringUtils.isNotEmpty(pageSize)) {
            page.setEveryPage(Integer.parseInt(pageSize));
        }

        CapPayListRepQueryCondition qc = new CapPayListRepQueryCondition();
        //TODO 设置查询条件？
        if (StringUtils.isNotEmpty(cstNm)) {
            qc.setCstNm(cstNm);
        }
        if (StringUtils.isNotEmpty(cstMgrOrgCdNm)) {
            qc.setCstMgrOrgCdNm(cstMgrOrgCdNm);
        }
        if (StringUtils.isNotEmpty(buOrgCd)) {
            qc.setBuOrgCd(buOrgCd);
        }
        if (StringUtils.isNotEmpty(prjTypCd)) {
            qc.setPrjTypCd(prjTypCd);
        }
        if (StringUtils.isNotEmpty(paySchNo)) {
            qc.setPaySchNo(paySchNo);
        }
        if (StringUtils.isNotEmpty(prInsNo)) {
            qc.setPrInsNo(prInsNo);
        }
        if (StringUtils.isNotEmpty(crtTmMin)) {
            qc.setCrtTmMin(crtTmMin);
        }
        if (StringUtils.isNotEmpty(crtTmMax)) {
            qc.setCrtTmMax(crtTmMax);
        }
        page.setCondition(qc);

        BaseLogUtils.info(logger, "initCapPayListRepList", "初始化CapPayListRep列表页面数据.DATA:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
        Pager pager = this.capPayListRepService.searchListPage(page);
        List<CapPayListRepVO> list = (List<CapPayListRepVO>) pager.getResultList();
        List<KeyValueVO> projectTypeList = this.prjBscInfoService.getProjectTypeList();
        List<KeyValueVO> orgList = this.cstBscInfoService.searchBusinessOrgList();
        List<SysOrgVO> sysOrgVOList = this.sysOrgService.searchListByOrgTypCd("3", "11000");
        Map<String, String> sysOrgVOMap = new HashMap<String, String>();
        for (SysOrgVO vo : sysOrgVOList) {
            sysOrgVOMap.put(vo.getOrgCd(), vo.getOrgNm());
        }
        Map<String, String> projectTypeMap = new HashMap<String, String>();
        Map<String, String> orgMap = new HashMap<String, String>();
        for (KeyValueVO keyValueVO : projectTypeList) {
            if (keyValueVO != null) {
                projectTypeMap.put(keyValueVO.getKey(), keyValueVO.getValue());
            }
        }
        for (KeyValueVO keyValueVO : orgList) {
            if (keyValueVO != null) {
                orgMap.put(keyValueVO.getKey(), keyValueVO.getValue());
            }
        }
        if (list != null && list.size() > 0) {
            for (CapPayListRepVO capPayListRepVO : list) {
                prInsNo = capPayListRepVO.getPrInsNo();
                String cstMgrOrgCd = capPayListRepVO.getCstMgrOrgCd();   //省公司
                buOrgCd = capPayListRepVO.getBuOrgCd();             //事业部
                String prjTypeCd = capPayListRepVO.getPrjTypCd();         //项目类型
                String buOrgCdNm = "";
                String prjTypCdNm = "";
                if (projectTypeList != null && projectTypeList.size() > 0) {
                    if (projectTypeMap.containsKey(prjTypeCd)) {
                        prjTypCdNm = projectTypeMap.get(prjTypeCd);
                    }
                }
                if (orgList != null && orgList.size() > 0) {
                    if (orgMap.containsKey(buOrgCd)) {
                        buOrgCdNm = orgMap.get(buOrgCd);
                    }
                }
                if (StringUtils.isNotEmpty(cstMgrOrgCd)) {
                    if (cstMgrOrgCd.length() > 12) {
                        cstMgrOrgCd = cstMgrOrgCd.substring(0, 12);
                    }
                    if (sysOrgVOMap.containsKey(cstMgrOrgCd)) {
                        cstMgrOrgCdNm = sysOrgVOMap.get(cstMgrOrgCd);
                    } else {
                        cstMgrOrgCdNm = "";
                    }

                }
                if (StringUtils.isNotBlank(prInsNo)) {
                    if (prInsNo.contains(".")) {
                        prInsNo = prInsNo.substring(prInsNo.indexOf(".") + 1);
                        capPayListRepVO.setPrInsNo(prInsNo);
                        capPayListRepVO.setBuOrgCdNm(buOrgCdNm);
                        capPayListRepVO.setCstMgrOrgCdNm(cstMgrOrgCdNm);
                        capPayListRepVO.setPrjTypCdNm(prjTypCdNm);
                    }
                }
            }
        }
        ListPageVO<CapPayListRepVO> listPage = new ListPageVO<CapPayListRepVO>(list, pager.getTotalCount());
        return listPage;
    }

    /**
     * 删除数据
     */
    @RequestMapping(value = "/removeByIds.do", method = {RequestMethod.POST})
    @ResponseBody
    public DataResultVO removeCapPayListRepByIds(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        String ids = request.getParameter("ids");
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_DELETE_SUCC);
        if (StringUtils.isEmpty(ids)) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
            BaseLogUtils.info(logger, "removeCapPayListRepByIds", "删除CapPayListRep数据,主键参数为空!", CurrentThreadContext.getCurrentUserName());
            return result;
        }

        List<Long> idsList = new ArrayList<Long>();
        JSONArray idsArray = JSONArray.fromObject(ids);
        for (Object object : idsArray) {
            idsList.add(Long.valueOf(object.toString()));
        }

        try {
            boolean success = this.capPayListRepService.removeByIds(idsList);
            if (!success) {
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo(SysLogConstant.LOG_CMM_DELETE_FAIL);
                BaseLogUtils.info(logger, "removeCapPayListRepByIds", "删除CapPayListRep数据失败,ID:" + idsList.toString(), CurrentThreadContext.getCurrentUserName());
            }
            BaseLogUtils.info(logger, "removeCapPayListRepByIds", "删除CapPayListRep数据成功,ID:" + idsList.toString(), CurrentThreadContext.getCurrentUserName());
        } catch (BaseException ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
            BaseLogUtils.info(logger, "removeCapPayListRepByIds", "删除CapPayListRep数据失败,ID:" + idsList.toString() + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        } catch (Exception ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
            BaseLogUtils.error(logger, "removeCapPayListRepByIds", "删除CapPayListRep数据失败,系统错误.ID:" + idsList.toString() + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        }
        return result;
    }

    /**
     * 得到一条记录
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getById.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object getCapPayListRepById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        String id = request.getParameter("id");
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
        if (StringUtils.isEmpty(id)) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
            BaseLogUtils.info(logger, "getCapPayListRepById", "获取CapPayListRep数据,主键参数为空.", CurrentThreadContext.getCurrentUserName());
            return result;
        }

        try {
            CapPayListRepVO vo = this.capPayListRepService.selectById(Long.valueOf(id));
            if (null == vo) {
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo(SysLogConstant.LOG_CMM_QUERY_DATA_IS_EMPTY);
                BaseLogUtils.info(logger, "getCapPayListRepById", "获取CapPayListRep数据不存在,ID:" + id, CurrentThreadContext.getCurrentUserName());
            }
            result.setData(vo);
            BaseLogUtils.info(logger, "getCapPayListRepById", "获取CapPayListRep数据成功,ID:" + id, CurrentThreadContext.getCurrentUserName());
        } catch (BaseException ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
            BaseLogUtils.info(logger, "getCapPayListRepById", "获取CapPayListRep数据失败,ID:" + id + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        } catch (Exception ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
            BaseLogUtils.error(logger, "getCapPayListRepById", "获取CapPayListRep数据失败,系统错误.ID:" + id + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        }
        return result;
    }

    /**
     * 新增
     */
    @RequestMapping(value = "/add.do", method = {RequestMethod.POST})
    @ResponseBody
    public Object addCapPayListRep(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC);
        CapPayListRepVO entity = null;//获取参数
        try {
            entity = this.getRequestParamForCapPayListRep(request);//获取参数
            Long id = this.capPayListRepService.add(entity);
            result.setData(id);
            BaseLogUtils.info(logger, "addCapPayListRep", "新增CapPayListRep数据成功,ID:" + id + ".DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
        } catch (BaseException ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR + ex.getMessage());
            BaseLogUtils.info(logger, "addCapPayListRep", "新增CapPayListRep数据失败.DATA:" + JSONObject.toJSONString(entity) + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        } catch (Exception ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR + ex.getMessage());
            BaseLogUtils.error(logger, "addCapPayListRep", "新增CapPayListRep数据失败,系统错误.DATA:" + JSONObject.toJSONString(entity) + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        }
        return result;
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/modify.do", method = {RequestMethod.POST})
    @ResponseBody
    public Object modifyCapPayListRep(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_UPDATE_SUCC);
        CapPayListRepVO entity = null;
        try {
            entity = this.getRequestParamForCapPayListRep(request);
            if (entity.getId() == null) {
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
                BaseLogUtils.info(logger, "modifyCapPayListRep", "修改CapPayListRep信息,主键参数为空！.", CurrentThreadContext.getCurrentUserName());
                return result;
            }

            boolean success = this.capPayListRepService.modify(entity);
            if (!success) {
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo(SysLogConstant.LOG_CMM_UPDATE_FAIL);
                BaseLogUtils.info(logger, "modifyCapPayListRep", "修改CapPayListRep数据失败.DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
                return result;
            }
            BaseLogUtils.info(logger, "modifyCapPayListRep", "修改CapPayListRep数据成功.DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
        } catch (BaseException ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR + ex.getMessage());
            BaseLogUtils.info(logger, "modifyCapPayListRep", "修改CapPayListRep数据失败.DATA:" + JSONObject.toJSONString(entity) + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        } catch (Exception ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR + ex.getMessage());
            BaseLogUtils.error(logger, "modifyCapPayListRep", "修改CapPayListRep数据失败,系统错误.DATA:" + JSONObject.toJSONString(entity) + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        }
        return result;
    }

    /**
     * 获取请求参数
     *
     * @param request 请求
     * @return
     */
    private CapPayListRepVO getRequestParamForCapPayListRep(HttpServletRequest request) throws BaseException {
        //获取参数
        String id = request.getParameter("id");//主键
        String prInsNo = request.getParameter("prInsNo");//流程编号
        String paySchNo = request.getParameter("paySchNo");//支付表号
        String cstMgrOrgCd = request.getParameter("cstMgrOrgCd");//省公司（客户经理组织架构）
        String cstMgrOrgCdNm = request.getParameter("cstMgrOrgCdNm");//省公司名称（客户经理组织架构）
        String ruTaskNm = request.getParameter("ruTaskNm");//null
        String prCrtTm = request.getParameter("prCrtTm");//创建时间
        String cstNm = request.getParameter("cstNm");//客户名称
        String rcptUnt = request.getParameter("rcptUnt");//收款单位/人
        String payeeBkNm = request.getParameter("payeeBkNm");//收款银行
        String accNo = request.getParameter("accNo");//收款账号
        String buOrgCd = request.getParameter("buOrgCd");//所属事业部
        String buOrgCdNm = request.getParameter("buOrgCdNm");//所属事业部名称
        String prjTypCd = request.getParameter("prjTypCd");//项目类型
        String prjTypCdNm = request.getParameter("prjTypCdNm");//项目类型名称
        String prdNm = request.getParameter("prdNm");//租赁物名称
        String prdAmt = request.getParameter("prdAmt");//设备单价
        String payAmt = request.getParameter("payAmt");//应付金额
        String fstRentAmt = request.getParameter("fstRentAmt");//应收首期租金
        String grtAmt = request.getParameter("grtAmt");//应收保证金

        //验证参数并赋值
        CapPayListRepVO entity = new CapPayListRepVO();
        //entity.setPrInsNo(prInsNo);//流程编号
        //entity.setPaySchNo(paySchNo);//支付表号
        //entity.setCstMgrOrgCd(cstMgrOrgCd);//省公司（客户经理组织架构）
        //entity.setCstMgrOrgCdNm(cstMgrOrgCdNm);//省公司名称（客户经理组织架构）
        //entity.setRuTaskNm(ruTaskNm);//null
        //entity.setPrCrtTm(prCrtTm);//创建时间
        //entity.setCstNm(cstNm);//客户名称
        //entity.setRcptUnt(rcptUnt);//收款单位/人
        //entity.setPayeeBkNm(payeeBkNm);//收款银行
        //entity.setAccNo(accNo);//收款账号
        //entity.setBuOrgCd(buOrgCd);//所属事业部
        //entity.setBuOrgCdNm(buOrgCdNm);//所属事业部名称
        //entity.setPrjTypCd(prjTypCd);//项目类型
        //entity.setPrjTypCdNm(prjTypCdNm);//项目类型名称
        //entity.setPrdNm(prdNm);//租赁物名称
        //entity.setPrdAmt(prdAmt);//设备单价
        //entity.setPayAmt(payAmt);//应付金额
        //entity.setFstRentAmt(fstRentAmt);//应收首期租金
        //entity.setGrtAmt(grtAmt);//应收保证金

//		if (StringUtils.isBlank(name)) {
//            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_604, "名称不能为空.");
//      }
//      if (StringUtils.isNotBlank(num)) {
//          if (!StringUtils.isUnsignedNumeric(num)) {
//                throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_604, "请填写正确的数字.");
//          }
//      }

        Date tm = new Date();
        if (StringUtils.isNotBlank(id)) {
            entity.setId(Long.valueOf(id));
            entity.setMdfTm(tm);
            entity.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
        } else {
            entity.setCrtTm(tm);
            entity.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
            //entity.setCrtUsrOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前用户组织机构代码
            //entity.setOrgCd(CurrentThreadContext.getCurrentBuOrgCd());//事业部组织机构代码
        }

        return entity;
    }

    /****************************
     * 导出资金放款报表
     ****************************/
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/exportCapLoanList.do", method = {RequestMethod.POST})
    @ResponseBody
    public DataResultVO exportCapLoanList(HttpServletRequest request, HttpServletResponse response) {
        BaseLogUtils.info(logger, "exportCapLoanList", "批量导出资金放款明细==");
        CapPayListRepQueryCondition qc = new CapPayListRepQueryCondition();
        String cstNm = request.getParameter("cstNm");//承租人
        String cstMgrOrgCdNm = request.getParameter("cstMgrOrgCdNm");//省公司
        String crtTmMin = request.getParameter("crtTmMin");//创建最小时间
        String crtTmMax = request.getParameter("crtTmMax");//创建最大时间
        String buOrgCd = request.getParameter("buOrgCd");//事业部
        String prjTypCd = request.getParameter("prjTypCd");//项目类型
        String paySchNo = request.getParameter("paySchNo");//支付表号
        String prInsNo = request.getParameter("prInsNo");//流程号

        if (StringUtils.isNotBlank(cstNm)) {
            qc.setCstNm(cstNm);
        }
        if (StringUtils.isNotBlank(cstMgrOrgCdNm)) {
            qc.setCstMgrOrgCdNm(cstMgrOrgCdNm.trim());
        }
        if (StringUtils.isNotBlank(buOrgCd)) {
            qc.setBuOrgCd(buOrgCd.trim());
        }
        if (StringUtils.isNotBlank(prjTypCd)) {
            qc.setPrjTypCd(prjTypCd.trim());
        }
        if (StringUtils.isNotBlank(paySchNo)) {
            qc.setPaySchNo(paySchNo.trim());
        }
        if (StringUtils.isNotBlank(prInsNo)) {
            qc.setPrInsNo(prInsNo.trim());
        }

        if (StringUtils.isNotBlank(crtTmMin)) {
            qc.setCrtTmMin(crtTmMin);
        }
        if (StringUtils.isNotBlank(crtTmMax)) {
            qc.setCrtTmMax(crtTmMax);
        }
        List<CapPayListRepVO> list = this.capPayListRepService.searchList(qc);
        List<KeyValueVO> projectTypeList = this.prjBscInfoService.getProjectTypeList();
        List<KeyValueVO> orgList = this.cstBscInfoService.searchBusinessOrgList();
        List<SysOrgVO> sysOrgVOList = this.sysOrgService.searchListByOrgTypCd("3", "11000");
        Map<String, String> sysOrgVOMap = new HashMap<String, String>();
        for (SysOrgVO vo : sysOrgVOList) {
            sysOrgVOMap.put(vo.getOrgCd(), vo.getOrgNm());
        }
        Map<String, String> projectTypeMap = new HashMap<String, String>();
        Map<String, String> orgMap = new HashMap<String, String>();
        for (KeyValueVO keyValueVO : projectTypeList) {
            if (keyValueVO != null) {
                projectTypeMap.put(keyValueVO.getKey(), keyValueVO.getValue());
            }
        }
        for (KeyValueVO keyValueVO : orgList) {
            if (keyValueVO != null) {
                orgMap.put(keyValueVO.getKey(), keyValueVO.getValue());
            }
        }
        if (list != null && list.size() > 0) {
            for (CapPayListRepVO capPayListRepVO : list) {
                prInsNo = capPayListRepVO.getPrInsNo();
                String cstMgrOrgCd = capPayListRepVO.getCstMgrOrgCd();   //省公司
                buOrgCd = capPayListRepVO.getBuOrgCd();             //事业部
                String prjTypeCd = capPayListRepVO.getPrjTypCd();         //项目类型
                String buOrgCdNm = "";
                String prjTypCdNm = "";
                if (projectTypeList != null && projectTypeList.size() > 0) {
                    if (projectTypeMap.containsKey(prjTypeCd)) {
                        prjTypCdNm = projectTypeMap.get(prjTypeCd);
                    }
                }
                if (orgList != null && orgList.size() > 0) {
                    if (orgMap.containsKey(buOrgCd)) {
                        buOrgCdNm = orgMap.get(buOrgCd);
                    }
                }
                if (StringUtils.isNotEmpty(cstMgrOrgCd)) {
                    if (cstMgrOrgCd.length() > 12) {
                        cstMgrOrgCd = cstMgrOrgCd.substring(0, 12);
                    }
                    if (sysOrgVOMap.containsKey(cstMgrOrgCd)) {
                        cstMgrOrgCdNm = sysOrgVOMap.get(cstMgrOrgCd);
                    } else {
                        cstMgrOrgCdNm = "";
                    }

                }
                if (StringUtils.isNotBlank(prInsNo)) {
                    if (prInsNo.contains(".")) {
                        prInsNo = prInsNo.substring(prInsNo.indexOf(".") + 1);
                        capPayListRepVO.setPrInsNo(prInsNo);
                        capPayListRepVO.setBuOrgCdNm(buOrgCdNm);
                        capPayListRepVO.setCstMgrOrgCdNm(cstMgrOrgCdNm);
                        capPayListRepVO.setPrjTypCdNm(prjTypCdNm);
                    }
                }
            }
        }
        DataResultVO dataResultVO = this.capPayListRepService.exportCapPayListRep(list);
        return dataResultVO;
    }

    @SuppressWarnings(value = "unchecked")
    @RequestMapping(value = "/exportExcelByIds.do", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public DataResultVO exportExcelByIds(HttpServletRequest request, HttpServletResponse response) {
        String ids = request.getParameter("ids");
        DataResultVO result = new DataResultVO();
        if (StringUtils.isEmpty(ids)) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
            BaseLogUtils.info(logger, "exportExcelByIds", "导出CapPayListRep数据,主键参数为空!", CurrentThreadContext.getCurrentUserName());
            return result;
        }

        List<Long> idsList = new ArrayList<Long>();
        JSONArray idsArray = JSONArray.fromObject(ids);
        for (Object object : idsArray) {
            idsList.add(Long.valueOf(object.toString()));
        }
        List<CapPayListRepVO> list = this.capPayListRepService.selectByIds(idsList);
        List<KeyValueVO> projectTypeList = this.prjBscInfoService.getProjectTypeList();
        List<KeyValueVO> orgList = this.cstBscInfoService.searchBusinessOrgList();
        List<SysOrgVO> sysOrgVOList = this.sysOrgService.searchListByOrgTypCd("3", "11000");
        Map<String, String> sysOrgVOMap = new HashMap<String, String>();
        for (SysOrgVO vo : sysOrgVOList) {
            sysOrgVOMap.put(vo.getOrgCd(), vo.getOrgNm());
        }
        String prInsNo = "";
        String buOrgCd = "";
        String cstMgrOrgCdNm = "";
        Map<String, String> projectTypeMap = new HashMap<String, String>();
        Map<String, String> orgMap = new HashMap<String, String>();
        for (KeyValueVO keyValueVO : projectTypeList) {
            if (keyValueVO != null) {
                projectTypeMap.put(keyValueVO.getKey(), keyValueVO.getValue());
            }
        }
        for (KeyValueVO keyValueVO : orgList) {
            if (keyValueVO != null) {
                orgMap.put(keyValueVO.getKey(), keyValueVO.getValue());
            }
        }
        if (list != null && list.size() > 0) {
            for (CapPayListRepVO capPayListRepVO : list) {
                prInsNo = capPayListRepVO.getPrInsNo();
                String cstMgrOrgCd = capPayListRepVO.getCstMgrOrgCd();   //省公司
                buOrgCd = capPayListRepVO.getBuOrgCd();             //事业部
                String prjTypeCd = capPayListRepVO.getPrjTypCd();         //项目类型
                String buOrgCdNm = "";
                String prjTypCdNm = "";
                if (projectTypeList != null && projectTypeList.size() > 0) {
                    if (projectTypeMap.containsKey(prjTypeCd)) {
                        prjTypCdNm = projectTypeMap.get(prjTypeCd);
                    }
                }
                if (orgList != null && orgList.size() > 0) {
                    if (orgMap.containsKey(buOrgCd)) {
                        buOrgCdNm = orgMap.get(buOrgCd);
                    }
                }
                if (StringUtils.isNotEmpty(cstMgrOrgCd)) {
                    if (cstMgrOrgCd.length() > 12) {
                        cstMgrOrgCd = cstMgrOrgCd.substring(0, 12);
                    }
                    if (sysOrgVOMap.containsKey(cstMgrOrgCd)) {
                        cstMgrOrgCdNm = sysOrgVOMap.get(cstMgrOrgCd);
                    } else {
                        cstMgrOrgCdNm = "";
                    }

                }
                if (StringUtils.isNotBlank(prInsNo)) {
                    if (prInsNo.contains(".")) {
                        prInsNo = prInsNo.substring(prInsNo.indexOf(".") + 1);
                        capPayListRepVO.setPrInsNo(prInsNo);
                        capPayListRepVO.setBuOrgCdNm(buOrgCdNm);
                        capPayListRepVO.setCstMgrOrgCdNm(cstMgrOrgCdNm);
                        capPayListRepVO.setPrjTypCdNm(prjTypCdNm);
                    }
                }
            }
        }
        result = this.capPayListRepService.exportCapPayListRep(list);
        return result;
    }

    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/exportExs.do", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Object exportExcel(HttpServletRequest req, HttpServletResponse resp) {
        Map result = new HashMap();
        result.put("success", 1);
        HSSFWorkbook wb = new HSSFWorkbook();
        try {
            req.setCharacterEncoding("UTF-8");
            resp.setCharacterEncoding("UTF-8");
            resp.setContentType("application/x-download");
            System.out.println("11111111111");
            String filedisplay = "资金放款报表.xls";
            filedisplay = URLEncoder.encode(filedisplay, "UTF-8");
            resp.addHeader("Content-Disposition", "attachment;filename=" + filedisplay);
            System.out.println("2222222222");
            HSSFSheet sheet = wb.createSheet("资金放款报表");
            sheet.setColumnWidth(0, 50 * 160);
            HSSFFont font = wb.createFont();
            font.setFontName("宋体");
            font.setFontHeightInPoints((short) 16);

            HSSFRow row = sheet.createRow((int) 0);
            System.out.println("3333333333333333333333");
            HSSFCellStyle style = wb.createCellStyle();
            style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            String[] colNm = new String[]{"11", "22", "33"};
            for (int i = 0; i < colNm.length; i++) {
                HSSFCell cell = row.createCell(i);
                cell.setCellValue(colNm[i]);
            }
            System.out.println("44444444444444444");
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("name", "11111");
            params.put("age", "22");
            params.put("sex", "男");
            Map<String, Object> params1 = new HashMap<String, Object>();
            params.put("name2", "娃儿");
            params.put("age3", "33");
            params.put("sex4", "女");
            List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
            resultList.add(params);
            resultList.add(params1);
            System.out.println("55555555555555555");
            for (int i = 0; i < resultList.size(); i++) {
                row = sheet.createRow((int) i + 1);
                Map<String, Object> map = resultList.get(i);
                int j = 0;
                for (Map.Entry<String, Object> entry : map.entrySet()) {
                    row.createCell(j).setCellValue(String.valueOf(entry.getValue()));
                    j++;
                }
            }
        } catch (UnsupportedEncodingException e) {
			BaseLogUtils.error(logger, "-", "异常", e);
        }
        OutputStream out = null;
        try {
            out = resp.getOutputStream();
            wb.write(out);
        } catch (Exception e) {
			BaseLogUtils.error(logger, "-", "异常", e);
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
			BaseLogUtils.error(logger, "-", "异常", e);
            }
        }
        System.out.println("666666666666666666666====" + result.get("success"));
        return result;
    }
}

